package com.egao.common.manage.school;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.egao.common.core.annotation.ApiPageParam;
import com.egao.common.core.annotation.OperLog;
import com.egao.common.core.web.BaseController;
import com.egao.common.core.web.JsonResult;
import com.egao.common.core.web.PageParam;
import com.egao.common.core.web.PageResult;
import com.egao.common.module.counsel.entity.CounselTeacher;
import com.egao.common.module.counsel.service.CounselTeacherService;
import com.egao.common.module.system.constants.UserConstants;
import com.egao.common.module.system.entity.User;
import com.egao.common.module.system.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * @author lym
 * @date 2022/2/10 15:28
 */
@Api(tags = "预约咨询 咨询师管理")
@RestController
@RequestMapping("${api}")
public class SchoolCounselTeacherController extends BaseController {

    @Autowired
    private CounselTeacherService counselTeacherService;
    @Autowired
    private UserService userService;

    @PreAuthorize("hasAuthority('school:counsel-teacher:list')")
    @ApiOperation(value = "咨询师管理列表")
    @ApiPageParam
    @GetMapping("/school/counsel-teacher/page")
    public PageResult<CounselTeacher> page(HttpServletRequest request) {
        PageParam<CounselTeacher> pageParam = new PageParam<>(request);
        return new PageResult<>(counselTeacherService.listPage(pageParam), pageParam.getTotal());
    }

    @PreAuthorize("hasAuthority('school:counsel-teacher:list')")
    @ApiOperation(value = "教师用户集合")
    @ApiPageParam
    @GetMapping("/school/counsel-teacher/user")
    public JsonResult user(String name) {
        if (StrUtil.isBlank(name)) {
            return JsonResult.ok();
        }

        List<User> userList = userService.list(new LambdaQueryWrapper<User>()
                .eq(User::getUserType, UserConstants.USER_TYPE_TEACHER)
                .like(User::getUsername, name).or().like(User::getName, name)
        );
        return JsonResult.ok().setData(userList);
    }

    @PreAuthorize("hasAuthority('sys:dict:insert')")
    @OperLog(value = "咨询师管理", desc = "添加", param = false, result = true)
    @ApiOperation("添加咨询师")
    @PostMapping("/school/counsel-teacher/insert")
    public JsonResult insert(@Valid @RequestBody CounselTeacher counselTeacher){
        if (counselTeacherService.save(counselTeacher)) {
            return JsonResult.ok("添加成功");
        }

        return JsonResult.error("添加失败");
    }

    @PreAuthorize("hasAuthority('sys:dict:update')")
    @OperLog(value = "咨询师管理", desc = "修改", param = false, result = true)
    @ApiOperation("修改咨询师")
    @PutMapping("/school/counsel-teacher/update")
    public JsonResult update(@Valid @RequestBody CounselTeacher counselTeacher){
        if (counselTeacherService.updateById(counselTeacher)) {
            return JsonResult.ok("修改成功");
        }

        return JsonResult.error("修改失败");
    }

    @PreAuthorize("hasAuthority('sys:dict:delete')")
    @OperLog(value = "咨询师管理", desc = "删除", param = false, result = true)
    @ApiOperation("删除咨询师")
    @DeleteMapping("/school/counsel-teacher/delete")
    public JsonResult delete(Integer id){
        if (counselTeacherService.removeById(id)) {
            return JsonResult.ok("删除成功");
        }

        return JsonResult.error("删除失败");
    }
}
